package com.meihua.baoleyi.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.view.CatLoadingView;


/**
 * Created by liming on 2017/10/17.
 */
public class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    public CallBack addreCallBack;
    private Toast mToast;
    protected MyApp app;
    protected SharedPreferences spUserInfo;
    private static final String CONFIG = "config";
    protected Context mContext;
    private CatLoadingView loadingDialog;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApp) getActivity().getApplicationContext();
        spUserInfo = app.getSharedPreferences(CONFIG, app.MODE_PRIVATE);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * Toast
     *
     * @param resId 资源信息
     */
    public void toast(int resId) {
        String msg = app.getResources().getString(resId);
        mToast = Toast.makeText(app, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * Toast
     *
     * @param msg 文字信息
     */
    public void toast(String msg) {
        mToast = Toast.makeText(app, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 直接跳转到另一个Activity界面
     *
     * @param classObj ***Activity.class
     */
    public void overlay(Class<?> classObj) {
        try {
            Intent intent = new Intent();
            intent.setClass(getActivity(), classObj);
            startActivity(intent);
        } catch (Exception e) {
        }
    }

    /**
     * 直接跳转到另一个Activity，并带有bundle参数
     *
     * @param classObj class对象
     * @param params   bundle参数
     */
    public void overlay(Class<?> classObj, Bundle params) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(getActivity(), classObj);
        intent.putExtras(params);
        startActivity(intent);
    }

    /**
     * 以satartActivityForResult方式启动另一个Activity
     *
     * @param classObj    class 对象
     * @param requestCode 请求码
     */
    public void overlay(Class<?> classObj, int requestCode) {
        Intent intent = new Intent(getActivity(), classObj);
        startActivityForResult(intent, requestCode);
    }


    public void overlay(Class<?> classObj, Bundle params, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setClass(getActivity(), classObj);
        intent.putExtras(params);
        //  startActivity(intent);
        startActivityForResult(intent, requestCode);
    }

    public interface CallBack {
        void success();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mToast != null) {
            mToast.cancel();
        }
    }

    /**
     * 显示加载视图
     */
    protected void showDialog() {
        try {
            if (loadingDialog == null) {
                loadingDialog = new CatLoadingView();
            }
            if (!loadingDialog.isVisible()) {
                loadingDialog.show(getActivity().getSupportFragmentManager(), TAG);
            }

        } catch (Exception e) {
        }
    }

    /**
     * 移除等待视图
     */

    protected void dismissDialog() {
        try {
            if (loadingDialog == null) {
                return;
            }
            loadingDialog.dismiss();
            loadingDialog = null;
        } catch (Exception e) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        loadingDialog = null;
    }
}
